package inno.data;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

 

public class DataReaderSingelton {
		
	private static DataReaderSingelton instance = new DataReaderSingelton();
	
	String filesPath;
	String fileNames;
	ArrayList<StockFullData> stocksFullData;
	Map<String, StockFullData> stocksMap;
	
	private DataReaderSingelton(){
	}
	
	public static synchronized DataReaderSingelton getInstance(){
		return instance;
	}
	
	public void DataReaderFirstinitialize(String datafolder){
		filesPath = datafolder;
		stocksFullData = new ArrayList<StockFullData>();
		stocksMap = new HashMap<String, StockFullData>();
	}
	
	public void initialize(String[] stockNames, int size){
		
		for (int i=0;i<size;i++){
			BufferedReader br = null;
			try {
				if (filesPath.equals("")){
					//br = new BufferedReader( new FileReader(stockNames[i]+".csv"));
					br = new BufferedReader( new FileReader(stockNames[i]));
				}
				else{
					//br = new BufferedReader( new FileReader(filesPath+"/"+stockNames[i]+".csv"));
					br = new BufferedReader( new FileReader(filesPath+"/"+stockNames[i]));
				}
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			StockFullData newStock = new StockFullData(stockNames[i]);
			StringTokenizer st = null;
			String line;
			try {
				while( (line = br.readLine()) != null){
					st = new StringTokenizer(line, ",");
					// jump on the stock name at each line
					st.nextToken();
					newStock.addDayData(Long.valueOf(st.nextToken()), Double.valueOf(st.nextToken()),
							Double.valueOf(st.nextToken()), Double.valueOf(st.nextToken()),
							Double.valueOf(st.nextToken()), Long.valueOf(st.nextToken()));
					}
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			stocksFullData.add(newStock);
			stocksMap.put(stockNames[i], newStock);
		}		
	}
	
	public Double getClosingPrice(String stockName, int day){
		StockFullData myData= stocksMap.get(stockName);
		if (null!=myData){
			return myData.getClosingPrice(day);
		}
		else{
			return 0.0;
		}
	}
	
	public Double getLastClosingPrice(String stockName){
		StockFullData myData= stocksMap.get(stockName);
		if (null!=myData){
			return myData.getLastClosingPrice();
		}
		else{
			return 0.0;
		}
	}
	
	public Double getOpenPrice(String stockName, int day){
		StockFullData myData= stocksMap.get(stockName);
		if (null!=myData){
			return myData.getOpenPrice(day);
		}
		else{
			return 0.0;
		}	
	}
	
	public Double getLowPrice(String stockName, int day){
		StockFullData myData= stocksMap.get(stockName);
		if (null!=myData){
			return myData.getLowPrice(day);
		}
		else{
			return 0.0;
		}	
	}
	
	public Double getHighPrice(String stockName, int day){
		StockFullData myData= stocksMap.get(stockName);
		if (null!=myData){
			return myData.getHighPrice(day);
		}
		else{
			return 0.0;
		}	
	}
	
	public ArrayList<Double> getAllClosingPrice(String stockName){
		StockFullData myData= stocksMap.get(stockName);
		if (null!=myData){
			return myData.getAllClosingPrice();
		}
		else{
			return null;
		}
	}
	
	public int getDaysSum(String stockName){
		StockFullData myData= stocksMap.get(stockName);
		if (null!=myData){
			return myData.getDaysSum();
		}
		else{
			return 0;
		}
	}
		
}
	
